package com.my.commonlib.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;

public class Md5 {
	/**
	 * 获取字节数组的MD5报文(返回字节数组)
	 * 
	 * @author agger
	 * @date 2014-7-7
	 * @param source 需要获取报文的字节数组
	 * @return byte[] MD5报文字节数组
	 */
	public static byte[] md5Bytes(byte[] source) {
		byte[] result = null;

		try {
			java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
			md.update(source);
			byte[] tmp = md.digest();
			if(tmp.length == 16){
				result = tmp;
			}
		} catch (Exception e) {
			result = null;
		}

		return result;
	}
	public static String byte2hex(byte[] bytes) {
		StringBuilder sign = new StringBuilder();
		for (int i = 0; i < bytes.length; i++) {
			String hex = Integer.toHexString(bytes[i] & 0xFF);
			if (hex.length() == 1) {
				sign.append("0");
			}
			// sign.append(hex.toUpperCase());
			sign.append(hex);
		}
		return sign.toString();
	}
	/**
	 * 获取字节数组的MD5报文
	 *
	 * @author agger
	 * @date 2014-7-7
	 * @param source 需要获取报文的字节数组
	 * @return String MD5报文
	 */
	public static String md5(byte[] source) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

		try {
			java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
			md.update(source);
			byte tmp[] = md.digest();
			char str[] = new char[16 * 2];
			int k = 0;

			for (int i = 0; i < 16; i++) {
				byte byte0 = tmp[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}

			return new String(str);
		} catch (Exception e) {
			return null;
		}
	}

	public String getString(int[] str){
		String strl = "";
	    for(int i =0;i<str.length;i++){
	    	strl+= str[i];
	    }
	    return strl;
	}
	/**
	 * 获取指定路径文件的MD5报文
	 *
	 * @author agger
	 * @date 2014-7-7
	 * @param filePath 需要读取的文件路径
	 * @return String MD5报文
	 */
	public static String md5_file(String filePath) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

		try {
			File tmpFile = new File(filePath);
			if (!tmpFile.isFile()) {
				return null;
			}

			FileInputStream fis = new FileInputStream(tmpFile);
			java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");

			byte[] buffer = new byte[1024];
			int read_len = 0;
			while ((read_len = fis.read(buffer)) != -1) {
				md.update(buffer, 0, read_len);
			}
			fis.close();

			byte tmp[] = md.digest();
			char str[] = new char[16 * 2];
			int k = 0;

			for (int i = 0; i < 16; i++) {
				byte byte0 = tmp[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}

			return new String(str);
		} catch (Exception e) {
			return null;
		}
	}
	
	public static String StringToMD5(String password) {
		String md5 = "";
		try {
			md5 = byte2hex(encryptMD5(password));
		} catch (IOException e) {
			md5 = null;
			e.printStackTrace();
		}
		
		return md5;
	}
	
	public static byte[] encryptMD5(String data) throws IOException {
		byte[] bytes = null;
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			bytes = md.digest(data.getBytes("UTF-8"));
		} catch (GeneralSecurityException gse) {
			// String msg = getStringFromException(gse);
			// throw new IOException(msg);
			bytes = null;
		}
		return bytes;
	}
	
	public final static String getMessageDigest(byte[] buffer) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
		try {
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(buffer);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			return null;
		}
	}
	
	private Md5(){}
}
